<template>
  <div>
    <ul class="mui-table-view">
      <li class="mui-table-view-cell mui-media" v-for="item in newslist" :key="item.id">
        <!-- 将a链接改造为路由链接 -->
        <!-- 当属性赋的值不只是固定的值时，就要利用绑定v-on设置属性值 -->
        <router-link :to="'/home/newsinfo/' + item.id">
          <!-- 记得给属性绑定值用v-bind或@ -->
          <img class="mui-media-object mui-pull-left" :src="item.img_url" />
          <div class="mui-media-body">
            <h1>{{ item.title }}</h1>
            <p class="mui-ellipsis">
              <span>发表时间:{{
                  item.add_time | dateFormat("YYYY-MM-DD HH:mm:ss")
                }}</span>
              <span>点击:{{ item.click }}</span>
            </p>
          </div>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newslist: [], // 默认在页面上挂载一个空数组
    };
  },
  created() {
    // 每当页面加载的时候就获取数据
    this.getNewsList();
  },
  methods: {
    async getNewsList() {
      // 获取新闻列表
      const { data } = await this.$http.get("/api/getnewslist");
      if (data.status === 0) {
        return (this.newslist = data.message);
      }
    },
  },
};
</script>

<style lang="scss",scoped>
.mui-media-body {
  h1 {
    font-size: 14px;
  }
  .mui-ellipsis {
    color: rgb(53, 44, 173);
    display: flex;
    justify-content: space-between;
    font-size: 13px;
  }
}
</style>